package com.lst.tools.custom.vo;


import com.lst.tools.custom.constants.ErrorCodeConstants;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 接口返回基础类
 *
 * @author: Frank
 * @date: 2019/09/24 13:51
 * @version: V1.0
 */
@Data
@NoArgsConstructor
public class JsonResponse<T> {
    private boolean success;
    private String errorCode;
    private String errorMessage;
    private T data;

    /**
     * 创建成功返回
     *
     * @param <T>
     * @return
     */
    public static <T> JsonResponse<T> success() {
        JsonResponse<T> response = new JsonResponse<T>();
        response.setSuccess(true);
        return response;
    }

    /**
     * 创建成功返回
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> JsonResponse<T> success(T t) {
        JsonResponse<T> response = new JsonResponse<T>();
        response.setSuccess(true);
        response.setData(t);
        return response;
    }


    /**
     * 创建成功返回
     *
     * @param errorMessage
     * @param <T>
     * @return
     */
    public static <T> JsonResponse<T> fail(String errorMessage) {
        JsonResponse<T> response = new JsonResponse<T>();
        response.setSuccess(false);
        response.setErrorCode(ErrorCodeConstants.PUBLIC_ERROR);
        response.setErrorMessage(errorMessage);
        return response;
    }

    /**
     * 创建错误返回
     *
     * @param errorCode
     * @param errorMessage
     * @param <T>
     * @return
     */
    public static <T> JsonResponse fail(String errorCode, String errorMessage) {
        JsonResponse<T> response = new JsonResponse<T>();
        response.setSuccess(false);
        response.setErrorCode(errorCode);
        response.setErrorMessage(errorMessage);
        return response;
    }
}
